package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.itemsetting.tools.MGCreateFiles;
import com.moleq.models.NetworkSetting;

public class NetWorkSettingService {
	private DatabaseHelper DBHelper;
	private Context mContext;
	private String errorString;
	private MGCreateFiles mMGCreateFiles = new MGCreateFiles();
	
	public NetWorkSettingService() {

	}

	public NetWorkSettingService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
		mContext = context;
	}
	
	public List<NetworkSetting> FindAll(){
		List<NetworkSetting> obj = new ArrayList<NetworkSetting>();
		Cursor cursor = DBHelper.rawQuery("select * from t_network_setting order by ns_station",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int ns_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_NO));
			String ns_name  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_NAME));
			String ns_ip  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_IP));
			int ns_station  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_STATION));
			String ns_type  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_TYPE));
			
			obj.add(new NetworkSetting(ns_no,ns_station,ns_name,ns_ip,ns_type));
		}
		cursor.close();
		return obj;
	}
	
	public NetworkSetting Find(int no){		
		NetworkSetting obj = null;
		Cursor cursor = DBHelper.rawQuery("select * from t_network_setting where ns_no  = "+ no,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int ns_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_NO));
			String ns_name  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_NAME));
			String ns_ip  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_IP));
			int ns_station  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_STATION));
			String ns_type  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_TYPE));
			
			obj = new NetworkSetting(ns_no,ns_station,ns_name,ns_ip,ns_type);
					
		}
		cursor.close();
		return obj;
	
	}
	
	public NetworkSetting FindNetorkSettingByStn(int stn){		
		NetworkSetting obj = null;
		Cursor cursor = DBHelper.rawQuery("select * from t_network_setting where ns_station  = "+ stn,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int ns_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_NO));
			String ns_name  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_NAME));
			String ns_ip  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_IP));
			int ns_station  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_STATION));
			String ns_type  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_TYPE));
			
			obj = new NetworkSetting(ns_no,ns_station,ns_name,ns_ip,ns_type);
		}
		cursor.close();
		return obj;
	}
	
	public String FindIPByStn(int stn){		
		String ip = "";
		Cursor cursor = DBHelper.rawQuery("select ns_ip from t_network_setting where ns_station  = "+ stn,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return ip;
		}
		while(cursor.moveToNext()){
			ip = cursor.getString(cursor.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_IP));
		}
		cursor.close();
		return ip;
	}
	
	public int getMaxNo(){		
		int no = 0;
		Cursor cursor = DBHelper.rawQuery("select Max(ns_no) from t_network_setting",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return no;
		}
		if(cursor.moveToNext()){
			no  = cursor.getInt(0);		
		}
		cursor.close();
		return no;
	
	}
	
	public boolean checkStation(String station,String no){		
		boolean check = false;
		Cursor cursor = DBHelper.rawQuery("select ns_station from t_network_setting where ns_station = "+ station +" and ns_no <> "+ no,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return check;
		}
		if(cursor.moveToNext()){
			check = true;
		}
		cursor.close();
		return check;
	
	}
	
	public boolean checkIp(String ip,String no){		
		boolean check = false;
		Cursor cursor = DBHelper.rawQuery("select ns_ip from t_network_setting where ns_ip = '"+ ip+"'" +" and ns_no <> "+ no,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return check;
		}
		if(cursor.moveToNext()){
			check = true;
		}
		cursor.close();
		return check;
	
	}
	
	public boolean checkIp(String ip){		
		boolean check = false;
		Cursor cursor = DBHelper.rawQuery("select ns_ip from t_network_setting where ns_ip = '"+ ip+"'",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return check;
		}
		if(cursor.moveToNext()){
			check = true;
		}
		cursor.close();
		return check;
	
	}
	
	public boolean Insert(NetworkSetting obj){
		boolean check = true;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "INSERT INTO t_network_setting values(null,"+obj.getNs_station()+",'"+obj.getNs_name()+"','"+obj.getNs_ip()+"','"+obj.getNs_type()+"',strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))";
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		} 
		return check;
	}
	
	
	public boolean Update(NetworkSetting obj){
		boolean check = true;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "Update t_network_setting set " +
				"ns_station = "+obj.getNs_station()
				+",ns_name = '"+obj.getNs_name()
				+"',ns_ip = '"+obj.getNs_ip()
				+"',ns_type = '"+obj.getNs_type()
				+"', update_datetime = strftime('%Y%m%d%H%M%S','now')" +
				" where ns_no = " +obj.getNs_no();
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		} 
		return check;
	}
	
	
	
	
	
	public boolean Delete(int no){
		boolean check = true;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "Delete from t_network_setting " ;
		String ls_where = "where ns_no  = "+no ;
		sql = sql+ls_where ;
	
		errorString = DBHelper.execSQL(sql);
		
		if(errorString.length()>0){
			check = false;
		}
		
		return check;

	}

	
	public boolean isExistStation(String stn) {
		boolean result = false;
		
		long count = 0;
		Cursor cursor = DBHelper.rawQuery("SELECT count(*) FROM " + DatabaseSchema.NetWorkSetting.SCHEMA_NAME +" WHERE " +DatabaseSchema.NetWorkSetting.NS_STATION + " = "+ stn ,null);
		cursor.moveToFirst();
		count = cursor.getLong(0);
		cursor.close();
		
		if (count > 0) result = true;
	
		return result;
	}

	public boolean isExistIpAddress(String ip) {
		boolean result = false;
		
		long count = 0;
		Cursor cursor = DBHelper.rawQuery("SELECT count(*) FROM " + DatabaseSchema.NetWorkSetting.SCHEMA_NAME +" WHERE "+DatabaseSchema.NetWorkSetting.NS_IP +" = "+ ip ,null);
		cursor.moveToFirst();
		count = cursor.getLong(0);
		cursor.close();
		
		if (count > 0) result = true;
	
		return result;
	}
	
	public boolean isExist(String stn,String ip) {
		boolean result = false;
		
		long count = 0;
		String strSql = "SELECT count(*) FROM " + DatabaseSchema.NetWorkSetting.SCHEMA_NAME 
				      +" WHERE "+DatabaseSchema.NetWorkSetting.NS_IP + " = '"+ ip 
				      +"' AND " + DatabaseSchema.NetWorkSetting.NS_STATION + " = "+ stn ;
		Cursor cursor = DBHelper.rawQuery(strSql,null);
		cursor.moveToFirst();
		count = cursor.getLong(0);
		cursor.close();
		
		if (count > 0) result = true;
	
		return result;
	}
	
	public boolean isExist(int stno) {
		boolean result = false;
		
		long count = 0;
		String strSql = "SELECT count(*) FROM " + DatabaseSchema.NetWorkSetting.SCHEMA_NAME 
				      +" WHERE "+DatabaseSchema.NetWorkSetting.NS_NO + " = "+ stno;
		Cursor cursor = DBHelper.rawQuery(strSql,null);
		cursor.moveToFirst();
		count = cursor.getLong(0);
		cursor.close();
		
		if (count > 0) result = true;
	
		return result;
	}
	// insert item or update item
	public boolean save(NetworkSetting obj){
		boolean result = false;
		NetworkSetting ns =null;
		
		
		if (isExist(obj.getNs_no())) {
			
//			NetworkSetting nstemp = Find(obj.getNs_no());
//			if (GlobalInfo.getInstance().isMGNetwork) {
//				mMGCreateFiles.createrFile(nstemp.toMGData("DELETE"));//UPDATE,DELETE
//			}
			
			result = this.Update(obj);
			
//			if (GlobalInfo.getInstance().isMGNetwork) {
//				 ns = Find(obj.getNs_no());
//				 ns.setNs_ip(nstemp.getNs_ip());
//				mMGCreateFiles.createrFile(ns.toMGData("UPDATE"));//UPDATE,DELETE
//			}	
//			
		}else{
			obj.setNs_no(this.getMaxNo() + 1);
			result = this.Insert(obj);
			
//			if (GlobalInfo.getInstance().isMGNetwork) {
//				 ns = Find(obj.getNs_no());
//				mMGCreateFiles.createrFile(ns.toMGData("INSERT"));//UPDATE,DELETE
//			}
//			
		}
		
		return result;
	}
	
	public long getAllCount(){
		long result = 0;
		try {
			Cursor cursor = DBHelper.rawQuery("SELECT count(*) FROM t_network_setting ",null);
			cursor.moveToFirst();
			result = cursor.getLong(0);
			cursor.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	
}
